package com.ruoyi.rms.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.rms.enums.SeatStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rms.domain.SeatMessage;
import com.ruoyi.rms.service.ISeatMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 餐桌信息Controller
 * @author wyl
 * @date 2024-03-07
 */
@RestController
@RequestMapping("/rms/seat")
public class SeatMessageController extends BaseController
{
    @Autowired
    private ISeatMessageService seatMessageService;

    /**
     * 查询餐桌信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rms:seat:list')")
    @GetMapping("/list")
    public TableDataInfo list(SeatMessage seatMessage)
    {
        startPage();
        List<SeatMessage> list = seatMessageService.selectSeatMessageList(seatMessage);
        return getDataTable(list);
    }

    /**
     * 查询餐桌信息列表 不分页
     */
//    @PreAuthorize("@ss.hasPermi('rms:seat:list')")
    @GetMapping("/allData")
    public AjaxResult allData(SeatMessage seatMessage)
    {
        // 只查询已开放的餐桌信息
        return AjaxResult.success(seatMessageService.selectOpenSeatMessageList(seatMessage));
    }

    /**
     * 查询餐桌信息列表 不分页
     */
//    @PreAuthorize("@ss.hasPermi('rms:seat:list')")
    @GetMapping("/freeSeat")
    public AjaxResult getFreeSeat(SeatMessage seatMessage)
    {
        // 只查询空闲的餐桌 用于排队
        seatMessage.setStatus(SeatStatus.FREE.getStatus());
        return AjaxResult.success(seatMessageService.selectSeatMessageList(seatMessage));
    }

    /**
     * 导出餐桌信息列表
     */
    @PreAuthorize("@ss.hasPermi('rms:seat:export')")
    @Log(title = "餐桌信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SeatMessage seatMessage)
    {
        List<SeatMessage> list = seatMessageService.selectSeatMessageList(seatMessage);
        ExcelUtil<SeatMessage> util = new ExcelUtil<SeatMessage>(SeatMessage.class);
        util.exportExcel(response, list, "餐桌信息数据");
    }

    /**
     * 获取餐桌信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:seat:query')")
    @GetMapping(value = "/{seatId}")
    public AjaxResult getInfo(@PathVariable("seatId") Long seatId)
    {
        return success(seatMessageService.selectSeatMessageBySeatId(seatId));
    }

    /**
     * 新增餐桌信息
     */
    @PreAuthorize("@ss.hasPermi('rms:seat:add')")
    @Log(title = "餐桌信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SeatMessage seatMessage)
    {
        return seatMessageService.insertSeatMessage(seatMessage) > 0 ? AjaxResult.success("添加成功") : AjaxResult.error("添加的餐桌编号与已开放的餐桌编号重复，添加失败");
    }

    /**
     * 修改餐桌信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:seat:edit')")
    @Log(title = "餐桌信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SeatMessage seatMessage)
    {
        return seatMessageService.updateSeatMessage(seatMessage) > 0 ? AjaxResult.success("修改成功") : AjaxResult.error("修改后的餐桌编号与已开放的餐桌编号重复，修改失败");
    }

    /**
     * 删除餐桌信息
     */
    @PreAuthorize("@ss.hasPermi('rms:seat:remove')")
    @Log(title = "餐桌信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{seatIds}")
    public AjaxResult remove(@PathVariable Long[] seatIds)
    {
        return toAjax(seatMessageService.deleteSeatMessageBySeatIds(seatIds));
    }
}
